/*
 * @lc app=leetcode.cn id=203 lang=javascript
 *
 * [203] 移除链表元素
 */

// @lc code=start
/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @param {number} val
 * @return {ListNode}
 */
var removeElements = function(head, val) {
  if (!head) return null
  let prev = head
  let succ = head.next
  while (succ) {
    let next = succ.next
    if (succ.val === val) {prev.next = succ.next}
    else {
      prev = prev.next
    }
    succ = next
  }
  if (head.val === val) head = head.next

  return head
};
// @lc code=end

